package com.chyb.cloud.exp;


import com.chyb.cloud.resp.ResultData;
import com.chyb.cloud.resp.ReturnCodeEnum;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Log4j2
//@RestControllerAdvice/*标记该类为 全局异常处理器，集中处理所有 @Controller 或 @RestController 抛出的异常。*/
/**配置全局异常处理*/
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)/*当控制器抛出 RuntimeException 时，此方法会被触发。*/
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)/*异常处理方法的返回值将附带此状态码。*/
    public ResultData exception(Exception e) {
        log.info("全局异常监测，报错信息err:", e);
        return ResultData.fail(ReturnCodeEnum.RC500.getCode(), ReturnCodeEnum.RC500.getMsg());
    }
}
